package com.hxl.dailyworksummary.action;

import com.hxl.dailyworksummary.ai.AIService;
import com.hxl.dailyworksummary.git.GitService;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class GenerateDailySummaryAction extends AnAction {

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        Project project = e.getRequiredData(CommonDataKeys.PROJECT);

        // 在后台线程执行任务
        new Task.Backgroundable(project, "Generating Daily Summary") {
            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                GitService gitService = new GitService();
                AIService aiService = new AIService();

                // 获取今天的提交记录
                List<String> commits = gitService.getTodaysCommits(project);

                // 使用AI进行总结
                String summary = String.valueOf(aiService.summarizeWithAI(commits));

                // 在UI线程中显示结果
                com.intellij.openapi.application.ApplicationManager.getApplication().invokeLater(() ->
                    Messages.showMessageDialog(
                            project,
                            summary,
                            "今日工作日报",
                            Messages.getInformationIcon()
                    )
                );
            }
        }.queue();
    }
}